﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Rhino.Mocks;
using Spaanjaars.ContactManager.Infrastructure.UnitOfWork;
using Spaanjaars.ContactManager.Model;
using Spaanjaars.ContactManager.Services.Implementations;
using Spaanjaars.ContactManager.Services.Interfaces;
using Spaanjaars.ContactManager.Services.Queries;
using Spaanjaars.ContactManager.Services.ViewModels;

namespace Spaanjaars.ContactManager.Tests.Service
{
    [TestFixture]
    public class When_searching_for_contact_people
    {
        private IContactPersonRepository _contactPersonRepository;
        private IUnitOfWork _uow;       
        private MockRepository _mocks;
        private IContactPersonSearchService _contactPersonSearchService;
        private IList<ContactPerson> _contactPeople;


        [SetUp]
        public void Context()
        {
            _mocks = new MockRepository();

            _contactPersonRepository = _mocks.StrictMock<IContactPersonRepository>();
            _uow = _mocks.StrictMock<IUnitOfWork>();

            _contactPersonSearchService = new ContactPersonSearchService(_contactPersonRepository, _uow);

            _contactPeople = new List<ContactPerson>();
            _contactPeople.Add(new ContactPerson()
                                   {
                                       Name = new Name("Scott", "Millet"),
                                       DateOfBirth = DateTime.Now
                                   });

            _contactPeople.Add(new ContactPerson()
                                    {
                                        Name = new Name("Lynsey", "Millett"),
                                        DateOfBirth = DateTime.Now
                                    });

            _contactPeople.Add(new ContactPerson()
                                    {
                                        Name = new Name("Columbo", "Millett"),
                                        DateOfBirth = DateTime.Now
                                    });

            _contactPeople.Add(new ContactPerson()
                                    {
                                        Name = new Name("Peter", "Pan"),
                                        DateOfBirth = DateTime.Now
                                    });
        }

        [Test]
        public void All_contact_people_will_be_returned_when_no_filter_is_applied()
        {
            using (_mocks.Record())
            {
                Expect.Call(_contactPersonRepository.FindAll(1, 100)).Return(_contactPeople);
            }
            using (_mocks.Playback())
            {
                var findPeopleQuery = new FindPeopleQuery();

                findPeopleQuery.PageCount = 100;
                findPeopleQuery.PageIndex = 1;

                var contactPeopleViewModels = _contactPersonSearchService.FindPeople(findPeopleQuery);

                Assert.That(contactPeopleViewModels.Count().Equals(_contactPeople.Count()));
                
                foreach (ContactPersonViewModel contactPerson in contactPeopleViewModels)
                {
                    Assert.That(_contactPeople.Where(cpEntity => cpEntity.Name.FullName == contactPerson.Name).Any());
                }
            }             
        }


    }
}
